<%@ Page Language="C#" MasterPageFile="~/master/admin.master" AutoEventWireup="true"
    CodeFile="state-tax-list.aspx.cs" Inherits="admin_state_tax_list" Title="Untitled Page" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Namespace="ECommerce.Library.Web" TagPrefix="ucc" %>
<%@ Register Src="~/controls/wucAdminValidator.ascx" TagName="AdminValidator" TagPrefix="wuc" %>
<asp:Content ID="cntMiddleContent" ContentPlaceHolderID="cphMiddleContent" runat="Server">
    <script type="text/javascript">

        var StateName_Renderer = function (value, p, record, rowIndex) {
            var categoryUrl = String.format("<%= Page.ResolveUrl(AutoGear.BLL.CustomSiteMap.StateTaxEdit) %>?sid={0}", record.data.StateTaxId);
            return String.format("<a href=\"{0}\" class=\"leftlink\" target=\"_blank\">{1}</a>", categoryUrl, value);
        };

        var Tax_Renderer = function (value) {
            return value + "%";
        }

        var IsActive_Renderer = function (value) {
            if (value == true)
                return "Yes";
            else
                return "No";
        }

        var addTax = function () {
            window.location = "<%= SiteGlobals.BaseUrl %>" + "admin/state-tax-add.aspx";
        }

        var CommandHandler = function (command, rowIndex, grid) {
            //showObj(grid);
            grid.getRowEditor().stopEditing();
            grid.getView().refresh();
            grid.getSelectionModel().selectRow(rowIndex);
            grid.getRowEditor().startEditing(rowIndex);
        }

        var afterEdit = function (e) {

            e.grid.stopEditing();

            // Call DirectMethod
            Polana.WebService.UpdateStateTax(e.record.data.StateTaxId,
                                                 e.record.data.Tax,
                                                 e.record.data.IsActive);
        };

        var categoryGrid;

        var deleteTaxes = function () {
            if (categoryGrid != null) {
                categoryGrid.getRowEditor().stopEditing();

                var s = categoryGrid.getSelectionModel().getSelections();
                if (s.length > 0) {

                    if (confirm("Are you sure you want to delete these taxes?")) {

                        var ids = "";

                        for (var i = 0, r; r = s[i]; i++) {
                            ids = ids + " " + r.data.StateTaxId;
                        }

                        // Call DirectMethod
                        Polana.WebService.DeleteTaxes(ids);
                    }
                }
            }
        }

        var searchTax = function (grid) {
            Polana.WebService.SearchTax();
        }

        var onDocumentReady = function (grid) {
            categoryGrid = grid;
        }

    </script>
    <ext:ResourceManager ID="ScriptManager1" runat="server" Theme="Gray">
        <Listeners>
            <DocumentReady Fn="onDocumentReady(#{GridPanel1})" />
        </Listeners>
    </ext:ResourceManager>
    <ext:Store ID="Store1" runat="server" RemoteSort="true" OnRefreshData="Store1_RefreshData">
        <AutoLoadParams>
            <ext:Parameter Name="sort" Value="StateName" Mode="Value" />
            <ext:Parameter Name="start" Value="0" Mode="Raw" />
            <ext:Parameter Name="limit" Value="20" Mode="Raw" />
        </AutoLoadParams>
        <Proxy>
            <ext:PageProxy />
        </Proxy>
        <Reader>
            <ext:JsonReader IDProperty="StateTaxId">
                <Fields>
                    <ext:RecordField Name="StateTaxId" />
                    <ext:RecordField Name="StateName" />
                    <ext:RecordField Name="Tax" />
                    <ext:RecordField Name="IsActive" Type="Boolean" />
                </Fields>
            </ext:JsonReader>
        </Reader>
    </ext:Store>
    <div class="admin-function-top inline">
        <ext:GridPanel ID="GridPanel1" runat="server" EnableViewState="true" AutoHeight="true"
            StoreID="Store1">
            <ColumnModel>
                <Columns>
                    <ext:Column Header="State Name" Width="100" DataIndex="StateName">
                        <Renderer Fn="StateName_Renderer" />
                    </ext:Column>
                    <ext:Column Header="Tax" Width="80" DataIndex="Tax">
                        <Renderer Fn="Tax_Renderer" />
                        <Editor>
                            <ext:NumberField ID="NumberField1" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:Column Header="Active" Width="50" DataIndex="IsActive">
                        <Renderer Fn="IsActive_Renderer" />
                        <Editor>
                            <ext:Checkbox ID="chkIsActive" runat="server" />
                        </Editor>
                    </ext:Column>
                    <ext:CommandColumn Width="50" DataIndex="SpecialInstruction">
                        <Commands>
                            <ext:GridCommand Icon="ApplicationEdit" CommandName="Edit" Text="Edit" />
                        </Commands>
                    </ext:CommandColumn>
                </Columns>
            </ColumnModel>
            <TopBar>
                <ext:Toolbar ID="SearchToolbar2" runat="server" AutoScroll="true">
                    <Items>
                        <ext:TextField ID="txtStateName" runat="server" EmptyText="State Name" />
                        <ext:Button ID="btnToolbarSearch" runat="server" Text="Search" Icon="Magnifier">
                            <Listeners>
                                <Click Handler="searchTax(#{GridPanel1});" />
                            </Listeners>
                        </ext:Button>
                        <ext:Button ID="btnAddGroup" runat="server" Text="Add State Tax" Icon="Add">
                            <Listeners>
                                <Click Handler="addTax();" />
                            </Listeners>
                        </ext:Button>
                        <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        <ext:Button ID="btnToolbarDelete" runat="server" Text="Delete Selected" Icon="Delete">
                            <Listeners>
                                <Click Handler="deleteTaxes();" />
                            </Listeners>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </TopBar>
            <BottomBar>
                <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="20" DisplayInfo="true"
                    DisplayMsg="Displaying taxes {0} - {1} of {2}" EmptyMsg="No taxes to display"
                    AutoWidth="true" />
            </BottomBar>
            <LoadMask ShowMask="true" />
            <SelectionModel>
                <ext:CheckboxSelectionModel ID="CheckboxSelectionModel1" runat="server" ColumnPosition="4"
                    AllowDeselect="true" CheckOnly="true" />
            </SelectionModel>
            <Plugins>
                <ext:RowEditor ID="rowedi" runat="server" SaveText="Update" ClicksToEdit="2" ErrorSummary="false"
                    ShowTooltip="={Ext.emptyFn}">
                    <Listeners>
                        <AfterEdit Fn="afterEdit" />
                    </Listeners>
                </ext:RowEditor>
            </Plugins>
            <Listeners>
                <Command Handler="CommandHandler(command, rowIndex, this)" />
            </Listeners>
            <View>
                <ext:GridView ID="GridView1" runat="server" ForceFit="true" />
            </View>
        </ext:GridPanel>
    </div>
    <script type="text/javascript">
        Ext.override(Ext.grid.GridView, {
            getEditorParent: function () {
                return document.body;
            }
        });
    </script>
</asp:Content>